package com.dataextend.controller;

import com.dataextend.dto.ActMissionDTO;
import com.dataextend.dto.RegularMissionDTO;
import com.dataextend.entity.*;
import com.alibaba.fastjson.JSONObject;
import com.dataextend.dto.ResultSegment;
import com.dataextend.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *  营销 运营相关接口
 */
@RestController
@RequestMapping("/v1/Market")
@CrossOrigin(origins={"http://localhost:8080"},allowCredentials="true")
public class MarketController {
    @Autowired
    private EmailService emailService;
    @Autowired
    private ShortMessageService shortMessageService;
    @Autowired
    private PushService pushService;
    @Autowired
    private MarketListService marketListService;
    @Autowired
    private MarketDetailService marketDetailService;


    /**
     * 行为触发-活动分析
     */
    @RequestMapping(value = "/act/mission/analysis", method = RequestMethod.GET)
    public ResultSegment actMissionAnalysis(@RequestParam(value = "missionid", required = false) String missionid) {
        JSONObject obj = marketListService.actMissionAnalysis(missionid);
        return new ResultSegment(true, 1, "查询成功", obj);
    }

    /**
     * 行为触发-修改状态
     */
    @RequestMapping(value = "/act/mission/change/state", method = RequestMethod.GET)
    public ResultSegment actMissionChangeState(@RequestParam(value = "missionId", required = false) String missionId,
                                                   @RequestParam(value = "status", required = false) Integer status) {
        marketListService.actMissionChangeState(missionId, status);
        return new ResultSegment(true, 1, "查询成功");
    }


    /**
     * 行为触发-获取任务id
     */
    @RequestMapping(value = "/act/mission/getid", method = RequestMethod.GET)
    public ResultSegment actMissionGetid() {
        String missionId = marketListService.actMissionGetid();
        return new ResultSegment(true, 1, "查询成功", missionId);
    }

    /**
     * 行为触发-列表页
     */
    @RequestMapping(value = "/act/mission/list", method = RequestMethod.GET)
    public ResultSegment actMissionList(@RequestParam(value = "name", required = false) String name,
                                            @RequestParam(value = "status", required = false) Integer status,
                                            @RequestParam(value = "pageno", required = false) Integer pageno,
                                            @RequestParam(value = "pagesize", required = false) Integer pagesize) {
        JSONObject obj = marketListService.actMissionList(name, status, pageno, pagesize);
        return new ResultSegment(true, 1, "查询成功", obj);
    }


    /**
     * 行为触发-创建任务
     */
    @RequestMapping(value = "/act/mission/create", method = RequestMethod.POST)
    public ResultSegment actMissionCreate(@RequestBody ActMissionDTO actMissionDTO) {
        marketListService.actMissionCreate(actMissionDTO);
        return new ResultSegment(true, 1, "查询成功");
    }




    /**
     * 投放渠道下拉菜单
     */
    @RequestMapping(value = "/channel/list", method = RequestMethod.GET)
    public ResultSegment channelList() {
        List<String> obj = marketListService.channelList();
        return new ResultSegment(true, 1, "查询成功", obj);
    }


    /**
     * 定时触发-活动分析
     */
    @RequestMapping(value = "/regular/mission/analysis", method = RequestMethod.GET)
    public ResultSegment regularMissionAnalysis(@RequestParam(value = "missionid", required = false) String missionid) {
        JSONObject obj = marketListService.regularMissionAnalysis(missionid);
        return new ResultSegment(true, 1, "查询成功", obj);
    }


    /**
     * 定时触发-人群筛选下拉菜单
     */
    @RequestMapping(value = "/regular/usergroup/list", method = RequestMethod.GET)
    public ResultSegment regularUsergroupList(@RequestParam(value = "name", required = false) String name) {
        List<JSONObject> obj = marketListService.regularUsergroupList(name);
        return new ResultSegment(true, 1, "查询成功", obj);
    }


    /**
     * 定时触发列表页
     */
    @RequestMapping(value = "/regular/mission/list", method = RequestMethod.GET)
    public ResultSegment regularMissionList(@RequestParam(value = "name", required = false) String name,
                                            @RequestParam(value = "status", required = false) Integer status,
                                            @RequestParam(value = "pageno", required = false) Integer pageno,
                                            @RequestParam(value = "pagesize", required = false) Integer pagesize) {
        JSONObject obj = marketListService.regularMissionList(name, status, pageno, pagesize);
        return new ResultSegment(true, 1, "查询成功", obj);
    }


    /**
     * 创建定时触发
     */
    @RequestMapping(value = "/regular/mission/create", method = RequestMethod.POST)
    public ResultSegment regularMissionCreate(@RequestBody RegularMissionDTO regularMissionDTO, ServletRequest request){
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        // 获取当前访问的用户
        String userObj = httpServletRequest.getHeader("Authorization").split("; ")[1].split("=")[1];
        JSONObject user = JSONObject.parseObject(userObj);
        regularMissionDTO.setCreator(user.getString("user"));
        marketListService.createRegularMission(regularMissionDTO);
        return new ResultSegment(true, 1, "查询成功");
    }


    /**
     * 定时触发修改状态
     */
    @RequestMapping(value = "/regular/mission/change/state", method = RequestMethod.GET)
    public ResultSegment regularMissionChangeState(@RequestParam(value = "missionId", required = false) String missionId,
                                              @RequestParam(value = "status", required = false) Integer status) {
        marketListService.regularMissionChangeState(missionId, status);
        return new ResultSegment(true, 1, "查询成功");
    }



    /**
     * 推送消息列表
     */
    @RequestMapping(value = "/marketList", method = RequestMethod.POST)
    public ResultSegment marketList(@RequestBody MarketListRequest marketListRequest){
        JSONObject result = marketListService.marketList(marketListRequest);
        return new ResultSegment(true, 1, "查询成功", result);
    }


    /**
     * 查看推送详情
     */
    @RequestMapping(value = "/marketDetail", method = RequestMethod.POST)
    public ResultSegment marketDetail(@RequestBody MarketDetailRequest marketDetailRequest){
        JSONObject result = marketDetailService.marketDetail(marketDetailRequest);
        return new ResultSegment(true, 1, "查询成功", result);
    }


    /**
     * 电子邮件接口
     */
    @RequestMapping(value = "/emailMarket", method = RequestMethod.POST)
    public ResultSegment emailMarket(@RequestBody EmailRequest emailrequest){
        JSONObject result = emailService.createGroup(emailrequest);
        return new ResultSegment(true, 1, "查询成功", result);
    }


    /**
     *  短信接口
     */
    @RequestMapping(value = "/shortMessageMarket", method = RequestMethod.POST)
    public ResultSegment shortMessageMarket(@RequestBody ShortMessageRequest shortMessageRequest){
        JSONObject result = shortMessageService.createGroup(shortMessageRequest);
        return new ResultSegment(true, 1, "查询成功", result);
    }


    /**
     *  消息通知接口
     */
    @RequestMapping(value = "/pushMarket", method = RequestMethod.POST)
    public ResultSegment pushMarket(@RequestBody PushRequest pushRequest){
        JSONObject result = pushService.createGroup(pushRequest);
        return new ResultSegment(true, 1, "查询成功", result);
    }



}
